package JzOffer.jz42;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 张取
 * @version 1.0
 * @date 2022/1/9 上午10:47
 * JzOffer.jz42
 */

public class Solution01 {
    public static void main(String[] args){
        RecentCounter01 recentCounter = new RecentCounter01();
        System.out.println(recentCounter.ping(1));
        System.out.println(recentCounter.ping(100));
        System.out.println(recentCounter.ping(3001));
        System.out.println(recentCounter.ping(3002));
    }
}

class RecentCounter01 {

    private Queue<Integer> queue;
    int frontElem;
    int left;
    int right;

    public RecentCounter01(){
        queue = new LinkedList<>();
    }

    public int ping(int t){
        //新数据入栈
        queue.offer(t);
        right = t;
        left = right - 3000;

         if(queue.peek() != null){
             frontElem = queue.peek();
         } else {
             return 0;
         }

         //将不再范围内的数据出栈
         while(frontElem < left){
            queue.poll();
            if(queue.peek() != null){
                frontElem = queue.peek();
            } else {
                return 0;
            }
        }
        return queue.size();
    }
}
